package script.esx;

import cn.com.betasoft.dsat.probe.groovy.SimException;
import cn.com.betasoft.dsat.probe.groovy.util.*;
/**
原生变量：g_col_pars g_col_other g_rets
原生变量：g_res(g_shc, g_dbcon)
样例
[btmon@vmware_host btmon]$ df -Pk  Pk:单位为KB，Pm:单位为MB
Filesystem         1024-blocks      Used Available Capacity Mounted on
/dev/hda2              1106832    974348     76260      93% /
/dev/hda1               101089      9172     86698      10% /boot
/dev/hda3              1004052    772648    180400      82% /home
none                    248444         0    248444       0% /dev/shm
/dev/hda6             35254444  22688596  10775004      68% /usr
/usr/nx_tom/down/CentOS-5.0-i386-bin-DVD.iso/CentOS-5.0-i386-bin-DVD.iso   3630332   3630332         0     100% /mnt/cdrom
[btmon@vmware_host btmon]$ df -Pi
Filesystem            Inodes   IUsed   IFree IUse% Mounted on
/dev/hda2             140832   52717   88115   38% /
/dev/hda1              26104      33   26071    1% /boot
/dev/hda3             127744   21929  105815   18% /home
none                   62111       1   62110    1% /dev/shm
/dev/hda6            4480448  583916 3896532   14% /usr
/usr/nx_tom/down/CentOS-5.0-i386-bin-DVD.iso/CentOS-5.0-i386-bin-DVD.iso       0       0       0    -  /mnt/cdrom
*/
def json = [];

def tjson = [:];
//获取数据
def String ret = g_shc.collect('''export LANG=C;df -Pm''');
if(ret.contains("not found")){
	throw new SimException(8004,"ESX.OSFileSystem:没有找到命令.");
}
if(ret.contains("Permission denied")){
	throw new SimException(8005,"ESX.OSFileSystem:没有权限执行命令.");
}

def matched = false;
ret.split("\n").each{
	def matcher = (it =~ /(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%\s+(\S+)/);
	if (matcher.matches()){
		matched = true;
		def free = (int)(matcher[0][4].toLong());//MB
		def used = (int)(matcher[0][3].toLong());
		def total = free + used;
		int usedRatio = 0;
		if (total > 0) usedRatio = Util.round(used*100/total, 0);//取整
		def dfline = ['FileSystem':matcher[0][1], 
						'Total':total, 
						'Free':free, 
						'UsedRatio': usedRatio, 
						'MountedOn':matcher[0][6], 
						'Used':used, 
						'InodeCount': 0,
						'InodeUsedRate': 0
					];
		tjson[matcher[0][1]] = dfline;
	}
};
if(!matched){
	throw new SimException(8006,"ESX.OSFileSystem:返回数据不匹配.");
}

ret = g_shc.collect('''export LANG=C;df -Pi''');
if(ret.contains("not found")){
	throw new SimException(8007,"ESX.OSFileSystem:2没有找到命令.");
}
if(ret.contains("Permission denied")){
	throw new SimException(8008,"ESX.OSFileSystem:2没有权限执行命令.");
}
matched = false;
ret.split("\n").each{
	matcher = (it =~ /(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)%\s+(\S+)/);
	if (matcher.matches()){
		matched = true;
		def mname = matcher[0][1];
		if (tjson.containsKey(mname)){
			tjson[mname]['InodeCount'] = matcher[0][2].toInteger();
			tjson[mname]['InodeUsedRate'] =  matcher[0][5].toInteger();
		}
	}
};
if(!matched){
	throw new SimException(8009,"ESX.OSFileSystem:2返回数据不匹配.");
}

tjson.each{ df -> json.add(df.value) };
g_rets = json;